//
//  ZZNewCommentViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/3/21.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZZNewCommentViewController.h"

#import "ZCommentTableViewCell.h"
#import "ZPostCommentView.h"
#import "TacticsDetailModel.h"
static NSString * const ZDiscussViewIdentifier = @"ZDiscussViewIdentifier";
static int _page;
@interface ZZNewCommentViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView * _maintableView;
    LZUserInfo *userInfo;
}
/** 评论数据数组 */
@property (nonatomic, strong) NSMutableArray <TacticsDetailModel*>*commentArray;


@end

@implementation ZZNewCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeInterface];
    [_maintableView.mj_header beginRefreshing];
    
    _commentArray = [NSMutableArray array];
    userInfo = [LZUserInfo getUserInfo];
}
- (void)setDid:(NSString *)did {
    _did = did;
    
//    NSLog(@"%@",_maintableView);
}
- (void)initlizeInterface {
    self.view.backgroundColor = [UIColor whiteColor];
    _maintableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _maintableView.dataSource = self;
    _maintableView.delegate = self;
    _maintableView.rowHeight = 85 * ysIZE;
    _maintableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_maintableView registerClass:[ZCommentTableViewCell class] forCellReuseIdentifier:ZDiscussViewIdentifier];
    
    [self.view addSubview:_maintableView];
    
    // 上拉加载下拉刷新
    __weak typeof(self) weakSelf = self;
    _maintableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf pullDownData];
        [_maintableView.mj_header endRefreshing];
    }];
    _maintableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
        [_maintableView.mj_footer endRefreshing];
    }];
    
    
        [_maintableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.width.height.top.equalTo(self.view);
        }];

    
}

//表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    return _commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ZDiscussViewIdentifier forIndexPath:indexPath];
    cell.tacticsDetailModel = _commentArray[indexPath.row];
    cell.selectionStyle = 0 ;
    return cell;
}

// lazy
// 下拉刷新方法
- (void)pullDownData {
    // 下拉刷新之前清除之前的数据
    _page = 1;
    [YJProgressHUD showProgress:@"努力加载中..." inView:self.view];
    NSDictionary *paramas = @{@"P":[NSString stringWithFormat:@"%d",_page],@"did":_did};
    __weak typeof(self) weakSelf = self;
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Consul/moreCmt" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"评论下拉刷新----%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            
            [weakSelf dealDataWithDict:dict];
            
        }else if ([dict[@"code"] intValue] == 0) {
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
        LZLog(@"%@",error);
    }];
    
    
}

// 上拉加载方法
- (void)loadMoreData {
    _page++;
    [YJProgressHUD showProgress:@"努力加载中..." inView:self.view];
    NSDictionary *paramas = @{@"P":[NSString stringWithFormat:@"%d",_page],@"did":_did};
    __weak typeof(self) weakSelf = self;
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Consul/moreCmt" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"就医策略详情上拉数据----%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            [weakSelf dealPullUpDataWithDict:dict];
            //[weakSelf setValueWithDict:dict];
        }else if ([dict[@"code"] intValue] == 0) {
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
            
        }
    } failure:^(NSError *error) {
        LZLog(@"%@",error);
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
}

// 拿到数据以后处理下拉刷新方法
- (void)dealDataWithDict:(NSDictionary *)dict {
    [_commentArray removeAllObjects];
    NSArray *tempArr = dict[@"list"];
    [_maintableView.mj_footer resetNoMoreData];
    
    if (tempArr.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (tempArr.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
    }

    
    for (NSDictionary *dict in tempArr) {
        TacticsDetailModel *commentItem = [[TacticsDetailModel alloc] initWithDict:[dict mutableCopy]];
        [_commentArray addObject:commentItem];
    }
    [_maintableView reloadData];
}


// 处理上拉加载方法
- (void)dealPullUpDataWithDict:(NSDictionary *)dict {
    NSArray *tempArr = dict[@"list"];
    [_maintableView.mj_footer resetNoMoreData];
    
    if (tempArr.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (tempArr.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
    }else  {
        for (NSDictionary *dict in tempArr) {
            TacticsDetailModel *commentItem = [[TacticsDetailModel alloc] initWithDict:[dict mutableCopy]];
            [_commentArray addObject:commentItem];
        }
    }
    [_maintableView reloadData];
}


@end
